### Why So Secretive? Unpacking Attitudes Towards Secrecy and Success
### Rachel Myrick
## Replication Data for The Journal of Politics

## Replication of Figures 1, 2, and 4 in manuscript
## Note: Figure 3 was created in Stata (see myrick2019_rep_main.dta)

# FIGURE 1 ----------------------------------------------------------------

## Figure 1: Figures that map CIA interventions during the Cold War
## The raw data is from Berger et al. (2013), AER

#Read in Berger et al. data of CIA/KGB Interventions
dat = read.csv("3_aer_cia_kgb.csv", stringsAsFactors = FALSE)

#Subset data
in.sup = subset(dat, US_install_and_support == 1, select = c(isocode))
sup.only = subset(dat, US_support_only == 1, select = c(isocode))

in.sup = unique(in.sup)
sup.only = unique(sup.only)

in.sup$install.support = 1
sup.only$support.only = 1

#Create country maps 

library(rworldmap)

is.map = joinCountryData2Map(in.sup, joinCode = "ISO3",
                              nameJoinColumn = "isocode")

is.map.pic = mapCountryData(is.map, nameColumnToPlot="install.support", catMethod = "categorical",
                             missingCountryCol = gray(.8), addLegend = FALSE, mapTitle= "CIA Interventions to Install and Support New Leader (1947-1989)")

sup.map = joinCountryData2Map(sup.only, joinCode = "ISO3",
                               nameJoinColumn = "isocode")

sup.map.pic = mapCountryData(sup.map, nameColumnToPlot="support.only", catMethod = "categorical",
                              missingCountryCol = gray(.8), addLegend = FALSE, mapTitle= "CIA Interventions to Support Existing Leaders (1947-1989)")


# FIGURE 2 ----------------------------------------------------------------

## Figure 2: Figures that show results of public opinion polls about historic possible covert actions
## This data is from iPOLL from the Roper Center for Public Opinion Research (see article for complete citations)

## Read in Poll Data from Roper iPOLL

ic = read.csv("3_covertpolls.csv", stringsAsFactors = FALSE)

## Iran Poll

iran = subset(ic, country == "Iran")
iran$order = c(3, 1, 2, 4, 5)

ggplot(data = iran, aes(x = reorder(action, -order), y = percent)) + geom_bar(stat = "identity", width = 0.5) +
    theme_minimal(base_size=20) + coord_flip() + ylim(0, 50) +
    ggtitle("Prevent Iran from Getting a Nuclear Weapon (2012)?") + ylab("") + xlab("")

## Colombia Poll
col = subset(ic, country == "Colombia" & action!="Other")
col$order = c(4, 3, 2, 1, 5)

ggplot(data = col, aes(x = reorder(action, -order), y = percent)) + geom_bar(stat = "identity", width = 0.5) +
    theme_minimal(base_size=20) + coord_flip() + ylim(0, 50) +
    ggtitle("Fight the War on Drugs in Colombia (1990)?") + ylab("") + xlab("")

## Nicaragua Poll
nic = subset(ic, country == "Nicaragua")
nic$order = c(1, 2, 3)

ggplot(data = nic, aes(x = reorder(action, -order), y = percent)) + geom_bar(stat = "identity", width = 0.5) +
    theme_minimal(base_size=20) + coord_flip() + ylim(0, 50) +
    ggtitle("Covert CIA Support for rebels in Nicaragua (1985)?") + ylab("Percent of Respondents") + xlab("")

# FIGURE 3 ----------------------------------------------------------------

## Replication of Figure 3 (main treatment effects) is in myrick2019_rep_main.dta
## Note: This figure was made in Stata

# FIGURE 4 ----------------------------------------------------------------

### Figure 4: Plots of Mean Approval by Secrecy and Success
### Results are pooled across the three main experiments

library(dplyr)
library(ggplot2)
library(stringr)
library(lubridate)

## Set working directory
setwd("")

#Read in Prepped Data

full1 = read.csv("2_exp1_data.csv", stringsAsFactors = FALSE)
full2 = read.csv("2_exp2_data.csv", stringsAsFactors = FALSE)
full3 = read.csv("2_exp3_data.csv", stringsAsFactors = FALSE)

#Prep for Merge

full1$negpub = rep(NA, nrow(full1))
full2$negpub = rep(NA, nrow(full2))
full1$covert_deceit = rep(NA, nrow(full1))
full2$covert_deceit = rep(NA, nrow(full2))
full2$military = rep(NA, nrow(full2))
full3$negpub = rep(NA, nrow(full3))
full3$military = rep(NA, nrow(full3))
full3$covert = full3$covert_deceit

# Merge Three Experiments

full = rbind(full1, full2, full3)

### Create SummarySE Function

summarySE = function(data=NULL, measurevar, groupvars=NULL, na.rm=FALSE,
                     conf.interval=.95, .drop=TRUE) {
    library(plyr)
    length2 <- function (x, na.rm=FALSE) {
        if (na.rm) sum(!is.na(x))
        else       length(x)
    }
    datac <- ddply(data, groupvars, .drop=.drop,
                   .fun = function(xx, col) {
                       c(N    = length2(xx[[col]], na.rm=na.rm),
                         mean = mean   (xx[[col]], na.rm=na.rm),
                         sd   = sd     (xx[[col]], na.rm=na.rm)
                       )
                   },
                   measurevar
    )
    datac <- rename(datac, c("mean" = measurevar))
    datac$se <- datac$sd / sqrt(datac$N) 
    ciMult <- qt(conf.interval/2 + .5, datac$N-1)
    datac$ci <- datac$se * ciMult
    
    return(datac)
}

# Get Summary Statistics

full_means = summarySE(full, measurevar="approve_after", groupvars=c("covert","success"), na.rm = TRUE)
full_means = full_means %>% mutate(low_ci = approve_after - ci,
                                   high_ci = approve_after + ci)

# Plot Summary Statistics

ggplot(full_means, aes(x = factor(success), y = approve_after, 
                       fill = factor(covert, labels = c("Not Covert", "Covert")))) + 
    geom_bar(position = position_dodge(), stat = "identity", 
             colour = "black", size = 0.5) + 
    geom_errorbar(aes(min = low_ci, ymax = high_ci),
                  size = 0.5, width = 0.2, position = position_dodge(0.9)) +
    ylab("Approval of U.S. Government Action") + 
    xlab("Outcome of Operation") +
    coord_cartesian(ylim = c(1,7)) +
    scale_y_continuous(breaks = 1:7*1) +
    scale_fill_grey() +
    labs(fill = "Secrecy") +
    scale_x_discrete(labels = c("Unsuccessful", "Successful")) +
    theme_minimal(base_size=20)
